package com.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.admin.common.utils.DateUtils;
import com.cloud.admin.common.utils.StringUtils;
import com.cloud.admin.mapper.SysUserOnlineMapper;
import com.cloud.admin.model.entity.SysUserOnline;
import com.cloud.admin.service.SysUserOnlineService;
import com.cloud.common.shiro.model.UserOnline;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 在线用户记录
 *
 * @author douhaichao code generator
 * @date 2019-07-09 10:26:38
 */
@Service
@AllArgsConstructor
public class SysUserOnlineServiceImpl extends ServiceImpl<SysUserOnlineMapper, SysUserOnline> implements SysUserOnlineService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public SysUserOnline selectOne(SysUserOnline req) {
        QueryWrapper queryWrapper = new QueryWrapper<SysUserOnline>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<SysUserOnline> selectAll(SysUserOnline req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<SysUserOnline> selectPage(Page page, SysUserOnline req) {
        IPage<SysUserOnline> list = baseMapper.selectPage(
                page,
                new QueryWrapper<SysUserOnline>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<SysUserOnline> selectPageByLike(Page page, SysUserOnline req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (!StrUtil.isEmpty(req.getSessionid())) {
            queryWrapper.like("sessionId", req.getSessionid());
        }

        if (!StrUtil.isEmpty(req.getLoginName())) {
            queryWrapper.like("login_name", req.getLoginName());
        }

        if (!StrUtil.isEmpty(req.getDeptName())) {
            queryWrapper.like("dept_name", req.getDeptName());
        }

        if (!StrUtil.isEmpty(req.getIpaddr())) {
            queryWrapper.like("ipaddr", req.getIpaddr());
        }

        if (!StrUtil.isEmpty(req.getLoginLocation())) {
            queryWrapper.like("login_location", req.getLoginLocation());
        }

        if (!StrUtil.isEmpty(req.getBrowser())) {
            queryWrapper.like("browser", req.getBrowser());
        }

        if (!StrUtil.isEmpty(req.getOs())) {
            queryWrapper.like("os", req.getOs());
        }

        if (!StrUtil.isEmpty(req.getStatus())) {
            queryWrapper.like("status", req.getStatus());
        }

        if (null != req.getStartTimestamp()) {
            queryWrapper.eq("start_timestamp", req.getStartTimestamp());
        }

        if (null != req.getLastAccessTime()) {
            queryWrapper.eq("last_access_time", req.getLastAccessTime());
        }

        if (null != req.getExpireTime()) {
            queryWrapper.eq("expire_time", req.getExpireTime());
        }
        IPage<SysUserOnline> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(SysUserOnline req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    /**
     * 查询会话集合
     *
     * @param online 会话信息
     */
    @Override
    public List<UserOnline> selectOnlineByExpired(Date expiredDate) {
        String lastAccessTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, expiredDate);
        return baseMapper.selectOnlineByExpired(lastAccessTime);
    }

    @Override
    public UserOnline selectOnlineById(String sessionid) {
        return baseMapper.selectOnlineById(sessionid);
    }

//    @Override
//    public void deleteOnlineById(String s) {
//        baseMapper.deleteOnlineById(s);
//    }
//
//    @Override
//    public void batchDeleteOnline(List<String> needOfflineIdList) {
//        baseMapper.batchDeleteOnline(needOfflineIdList);
//    }

    /**
     * 通过会话序号删除信息
     *
     * @param sessionId 会话ID
     * @return 在线用户信息
     */
    @Override
    public void deleteOnlineById(String sessionId) {
        UserOnline userOnline = selectOnlineById(sessionId);
        if (StringUtils.isNotNull(userOnline)) {
            baseMapper.deleteOnlineById(sessionId);
        }
    }

    /**
     * 通过会话序号删除信息
     *
     * @param sessions 会话ID集合
     * @return 在线用户信息
     */
    @Override
    public void batchDeleteOnline(List<String> sessions) {
        for (String sessionId : sessions) {
            if (StrUtil.isEmpty(sessionId)) {
                continue;
            }
            UserOnline userOnline = selectOnlineById(sessionId);
            if (userOnline != null) {
                baseMapper.deleteOnlineById(sessionId);
            }
        }
    }

    /**
     * 保存会话信息
     *
     * @param online 会话信息
     */
    @Override
    public void saveOnline(UserOnline online) {
        if (null == online.getSessionId()) {
            return;
        }
        baseMapper.saveOnline(online);
    }
}
